﻿using System.Collections.Generic;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Net;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc;
using wojilu.Web.Mvc.Attr;
using YBBYunQi;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class HomeController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }
        private static readonly ILog logger = LogManager.GetLogger(typeof(HomeController));

        public HomeController()
        {
            logService = new SiteLogService();
        }

        [HttpGet]
        public virtual void Admin_Center()
        {
            SetPublic();
            set("jsstrlist", "$(document).ready(function(){sdmenuvol();});");
            set("hellostr", time.RemindTime());
            set("viewer.Name", ctx.viewer.obj.Name);
            set("viewer.RealName", (ctx.viewer.obj as User).RealName);
            set("version", MvcConfig.Instance.Version);
            set("logout", to(new AdminController().Logout));
            set("index", to(new QycmsController().Index));
            User u = (User)ctx.viewer.obj;
            bind("memberinfo", u);
            bind("powername", PowerGroup.findById(u.GroupId).powername);
            List<Lng> l = Lng.find("isopen=:i order by pid asc,id desc").set("i", 1).list();
            bindList("lngs", "lng", l);

        }

        [HttpGet]
        public virtual void SysSetting()
        {
            target(SysSettingSave);
            set("IframeName", ctx.Get("IframeName"));
            set("IframeHeightWindow", ctx.Get("IframeHeightWindow"));
            set("IframeWidthWindow", ctx.Get("IframeWidthWindow"));
            SetPublic();
            set("load", to(new AdminController().Load) + "?");
            var GroupId = ctx.GetInt("groupid") == 0 ? 1 : ctx.GetInt("groupid");
            set("groupid", GroupId);
            set("maildemo", "");
        }

        [HttpPost]
        public virtual void SysSettingSave()
        {
            for (int key = 1; key < 100; key++)
            {
                var list = Configs.find("groupid=:g").set("g", key).list();
                foreach (var row in list)
                {
                    if (row.isline != 1)
                    {
                        if (row.valtype == "editor")
                        {
                            var value = ctx.PostHtmlAll(row.valname);
                            if (row.value != value)
                            {
                                Configs.updateBatch("valuecontent='" + value + "'", "valname='" + row.valname + "'");
                                switch (row.valname)
                                {
                                    case "close_content":
                                        config.Instance.Site.Update("CloseReason", value);
                                        break;
                                }
                            }
                        }
                        else
                        {
                            var value = ctx.Post(row.valname);
                            if (row.value != value)
                            {
                                Configs.updateBatch("valuecontent='" + value + "'", "valname='" + row.valname + "'");
                                switch (row.valname)
                                {
                                    case "sitename":
                                        config.Instance.Site.Update("SiteName", value);
                                        break;

                                    case "is_close":
                                        config.Instance.Site.Update("IsClose", value == "1" ? true : false);
                                        break;

                                    case "domain":
                                        value = strUtil.TrimStart(value, "http://");
                                        value = strUtil.TrimEnd(value, "/");
                                        config.Instance.Site.Update("SiteUrl", value);
                                        break;

                                    case "icpbeian":
                                        config.Instance.Site.Update("BeiAn", value);
                                        break;

                                    case "admine_mail":
                                        config.Instance.Site.Update("Email", value);
                                        break;

                                    case "input_author":
                                        config.Instance.Site.Update("Webmaster", value);
                                        break;

                                    case "is_email":
                                        config.Instance.Site.Update("EnableEmail", value == "1" ? true : false);
                                        break;

                                    case "smtp_server":
                                        config.Instance.Site.Update("SmtpUrl", value);
                                        break;

                                    case "smtpenablessl":
                                        config.Instance.Site.Update("SmtpEnableSsl", value);
                                        break;

                                    case "smtp_username":
                                        config.Instance.Site.Update("SmtpUser", value);
                                        break;

                                    case "smtp_password":
                                        config.Instance.Site.Update("SmtpPwd", value);
                                        break;

                                    case "is_html":
                                        ConfigUtil.SetSetting("is_html", value);
                                        break;

                                }
                            }
                        }
                    }
                }
            }
            config.Reset();
            logService.Add((User)ctx.viewer.obj, "系统设置修改成功", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void MailDemo()
        {
            if (config.Instance.Site.EnableEmail == false)
            {
                echo("对不起，邮件服务尚未开启，无法发送邮件");
                return;
            }
            MailClient mail = MailClient.Init();
            Result sentResult = mail.Send(config.Instance.Site.Email, "YBBCMS邮箱发送测试！", "恭喜！您的系统邮件发送测试成功，如有问题请登陆：http://bbs.yibuban.com进行留言！");
            if (sentResult.IsValid)
            {
                logger.Info(wojilu.lang.get("sentok") + ": " + config.Instance.Site.Email);
                ctx.web.ResponseWrite("true");
                ctx.web.ResponseEnd();
            }
            else
            {
                logger.Info(wojilu.lang.get("exSentError") + ": " + config.Instance.Site.Email);
                ctx.web.ResponseWrite("false");
                ctx.web.ResponseEnd();
            }
        }

        [HttpPost]
        public virtual void SoftUpdate()
        {
            ctx.web.ResponseWrite("");
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void authentication()
        {
            ctx.web.ResponseWrite("");
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void windowclass()
        {
            ctx.web.ResponseWrite("false");
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void SoftKey()
        {
            ///已授权为true，未授权为False
            ctx.web.ResponseWrite("该系统已授权！");
            ctx.web.ResponseEnd();

            //var obj = YBB.Pager.YBBPage.GetCache("softkey");
            //if (PageValidator.IsNull(obj) || YBB.Pager.YBBPage.GetCache("softkey") != "1")
            //{
            //    var args = new Hashtable();
            //    args["Url"] = UrlOper.GetHost();
            //    string url = PHP.Serialize(args, true);
            //    string mess = CWebClient.GetPage("http://licence.yibuban.com/Licence/CheckSoftKey.aspx?UCode=" + url, Encoding.UTF8);
            //    Hashtable args1 = PHP.UnSerialize(mess);
            //    if (PageValidator.IsNull(args1["mess"]))
            //    {
            //        YBB.Pager.YBBPage.DelCache("softkey");
            //        Response.Write(mess);
            //        Response.End();
            //    }
            //    else
            //    {
            //        YBB.Pager.YBBPage.SCache("softkey", "1");
            //        Response.Write(args1["mess"]);
            //        Response.End();
            //    }
            //}
        }

    }
}
